# -*- coding: utf-8 -*-
# created on 2016/6/3


from mathsolver.functions.base import *
from mathsolver.functions.sympy_utils import get_all_child
from sympy.core.function import UndefinedFunction
from mathsolver.functions.hanshu.zhiyu import HanShuZhiYu


class FangChenHengChenLiFunc(BaseFunction):
    def solver(self, *args):
        f1, op, f2 = args[0].sympify()
        zhiyu = None
        for f in [f1, f2]:
            funcs = get_all_child(f, func=lambda xx: isinstance(type(xx), UndefinedFunction))
            for func in funcs:
                function = self.search(func.__class__.__name__)
                expr = function.run(args[1])
                stepsolver = HanShuZhiYu().solver(BaseEq(["y", expr]), args[1])
                zhiyu = stepsolver.output[0]
                self.steps += stepsolver.steps
                self.label.update(stepsolver.label)

        ineq = Rel(f1, f2, op)
        if op == ">=":
            ineq_latex = new_latex(ineq).replace("{\\left (", "(").replace("\\right )}", ")")
            ineq_latex_min = new_latex(ineq).replace("{\\left (", "_{min}(").replace("\\right )}", ")")
            self.steps.append(["", "∵ %s 对一切 %s 成立" % (ineq_latex, args[1].printing())])
            self.steps.append(["", "∴ %s(%s)" % (ineq_latex_min, args[1].printing())])
            left, right, lopen, ropen = zhiyu.args
            if lopen and not left.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(left), "\\ge", new_latex(f2))])
                self.output.append(BaseIneq([left, ">=", f2]))
            elif not lopen and not left.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(left), "\\ge", new_latex(f2))])
                self.output.append(BaseIneq([left, ">=", f2]))
            elif left.is_infinite:
                print("题目出错啦！！！")
        elif op == ">":
            ineq_latex = new_latex(ineq).replace("{\\left (", "(").replace("\\right )}", ")")
            ineq_latex_min = new_latex(ineq).replace("{\\left (", "_{min}(").replace("\\right )}", ")")
            self.steps.append(["∵ %s对一切%s成立" % (ineq_latex, args[1].printing()),
                               "∴ %s(%s)" % (ineq_latex_min, args[1].printing())])
            left, right, lopen, ropen = zhiyu.args
            if lopen and not left.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(left), ">=", new_latex(f2))])
                self.output.append(BaseIneq([left, ">=", f2]))
            elif not lopen and not left.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(left), ">", new_latex(f2))])
                self.output.append(BaseIneq([left, ">", f2]))
            elif left.is_infinite:
                print("题目出错啦！！！")
        elif op == "<=":
            ineq_latex = new_latex(ineq).replace("{\\left (", "(").replace("\\right )}", ")")
            ineq_latex_max = new_latex(ineq).replace("{\\left (", "_{max}(").replace("\\right )}", ")")
            self.steps.append(["∵ %s对一切%s成立" % (ineq_latex, args[1].printing()),
                               "∴ %s(%s)" % (ineq_latex_max, args[1].printing())])
            left, right, lopen, ropen = zhiyu.args
            if ropen and not right.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(left), "<=", new_latex(f2))])
                self.output.append(BaseIneq([right, "<=", f2]))
            elif not ropen and not right.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(right), "<=", new_latex(f2))])
                self.output.append(BaseIneq([right, "<=", f2]))
            elif right.is_infinite:
                print("题目出错啦！！！")
        elif op == "<":
            ineq_latex = new_latex(ineq).replace("{\\left (", "(").replace("\\right )}", ")")
            ineq_latex_max = new_latex(ineq).replace("{\\left (", "_{max}(").replace("\\right )}", ")")
            self.steps.append(["∵ %s对一切%s成立" % (ineq_latex, args[1].printing()),
                               "∴ %s(%s)" % (ineq_latex_max, args[1].printing())])
            left, right, lopen, ropen = zhiyu.args
            if ropen and not right.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(right), "<=", new_latex(f2))])
                self.output.append(BaseIneq([right, "<=", f2]))
            elif not ropen and not right.is_infinite:
                self.steps.append(["", "∴ %s %s %s" % (new_latex(right), "<", new_latex(f2))])
                self.output.append(BaseIneq([right, "<", f2]))
            elif right.is_infinite:
                print("题目出错啦！！！")
        return self


if __name__ == '__main__':
    pass
